﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace InSys.Documents
{
    public partial class FormPersonList : Form
    {
        private MapTestLinqDataContext db = new MapTestLinqDataContext(Properties.Settings.Default.testlinqConnectionString);
        public FormPersonList()
        {
            InitializeComponent();
            var queryPerson = from item in db.persons select item;
            personBindingSource.DataSource = queryPerson;
            var queryPost = from item in db.posts select item;
            postBindingSource.DataSource = queryPost;
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            db.SubmitChanges();
        }

        private void personDataGridView_CellParsing(object sender, DataGridViewCellParsingEventArgs e)
        {
            //int s =  (null == e.Value) ? 0 : (int)e.Value;
            ////OfType is needed because the employeeBindingSource returns instances of type object.

            //post Post = (from post in this.postBindingSource.OfType<post>()
            //             where post.id_post == s
            //             select post).FirstOrDefault(); 

            //e.Value = Post;
            //e.ParsingApplied = true;

        }

        private void personDataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {

        }

        private void personDataGridView_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
        {
        //    e.Row.Cells["dataGridViewTextBoxColumnId_post"].Value = 1;
        }

        private void toolStripButtonEdit_Click(object sender, EventArgs e)
        {
            int id_person = 0;
            id_person = null == personDataGridView.CurrentRow.DataBoundItem ? 0 : (personDataGridView.CurrentRow.DataBoundItem as InSys.Documents.person).id_person;
            if (personDataGridView.CurrentRow.IsNewRow)
                id_person = 0;
            FormPerson form = new FormPerson(id_person);
            form.Show();
        }
    }
}
